package dbutils02;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.junit.Before;
import org.junit.Test;

import java.sql.SQLException;
import java.util.HashMap;

/*
* 利用DBUtils做查询操作
*
* */
public class DBUtilsDemo3 {
    private QueryRunner qr;
    @Before
    public void  initQueryRunnner(){
        qr = new QueryRunner(JDBCUtils.getDataSource());
    }
    @Test
    public void  testBeanHander() throws Exception {
        Product p = qr.query("SELECT * From product where pid=?",
                new BeanHandler<Product>(Product.class),
                8);
        System.out.println(p);
    }
    @Test
    public void  testBeanHander02() throws Exception {
        HashMap<String, String> colField = new HashMap<>();
        Product p = qr.query("SELECT * From product where pid=?",
                new BeanHandler<Product>(Product.class,new BasicRowProcessor(new BeanProcessor(colField))),
                8);
        System.out.println(p);
    }
    @Test
    public void  testBeanHander03() throws Exception {

        Product p = qr.query("SELECT * From product where pid=?",
                new BeanHandler<Product>(Product.class,new BasicRowProcessor(new GenerousBeanProcessor())),
                8);
        System.out.println(p);
    }
}
